"""
@Project    : crnn
@Module     : draw_loss.py
@Author     : wangqinggang@haier.com
@Created    : 2020/12/9 9:22
@Desc       : 
"""
import numpy as np
multiword =[]
lesswords = []
errorwords = []
correctwords = []
with open('savaloss.txt','r',encoding='utf-8') as f:
    lines = f.readlines()
    for line in lines:
        line = line.replace('\n','').split(' ')
        lab = line[0]
        loss = float(line[1])/10000.0
        if lab=='1':
            multiword.append(loss)
        elif lab=='2':
            lesswords.append(loss)
        elif lab =='3':
            errorwords.append(loss)
        else:
            correctwords.append(loss)
#折线图
n = max(len(multiword),len(lesswords),len(errorwords),len(correctwords))
# if len(multiword)<n:
#     for i in range(n-len(multiword)):
#         multiword.append(0.0)
# if len(lesswords)<n:
#     for i in range(n-len(lesswords)):
#         lesswords.append(0.0)
# if len(errorwords)<n:
#     for i in range(n-len(errorwords)):
#         errorwords.append(0.0)
# if len(correctwords)<n:
#     for i in range(n-len(correctwords)):
#         correctwords.append(0.0)
# multiword_1 = multiword.copy()
# multiword_1.sort(reverse=True)
# # lesswords.sort(reverse=True)
# # errorwords.sort(reverse=True)
# # correctwords.sort(reverse=True)
# #显示中文
# import matplotlib.pyplot as plt
# plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签
# plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
# x = [i for i in range(len(multiword))]
# # y1 =[0.340,0.587,0.291,0.232,0.214]
# # y2 =[0.414,0.398,0.156,0.180,0.211]
# # y3 =[0.335,0.026,0.173,0.220,0.301]
# # y4 =[0.085,0.030,0.018,0.217,0.289]
#
# plt.figure(figsize = (50, 180)) #设置图像大小，当然可以设成方形（12,8）挺合适
# plt.plot(x, multiword, 'r',label = '多字') #作图，设置标签、线条颜色、线条大小
# plt.plot(x, multiword_1, 'g', label = '多字排序')
# # plt.plot(x, errorwords, 'b-.', label = '错字', linewidth = 2.5)
# # plt.plot(x, correctwords, 'k:', label = '正确字', linewidth = 2.5)
#
# plt.plot(x, multiword, 'or',markersize = 1) #作图，设置标签、线条颜色、线条大小
# plt.plot(x, multiword_1, '*g',markersize = 1)
# # plt.plot(x, errorwords, 'Db',markersize = 8)
# # plt.plot(x, correctwords, '^k',markersize = 8)
#
# ax = plt.subplot(111) #这是画布哦，说明只在一张图显示，也可分割多图
# plt.xticks(fontsize=20)#调调字体
# plt.yticks([0,0.001,0.005,0.01,0.05,0.1,0.2,0.3,0.4,0.5,0.5,0.6,0.7,0.8,0.9,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,8,11,15,20,30,40,50,60,70,100],fontsize=10)
#
# plt.xlabel('点数 ', fontsize=25) # x轴名称
# plt.ylabel('loss', fontsize=25) # y轴名称
# # plt.title('A Simple Example') #标题
# plt.ylim(0, 180) #显示的y轴范围
# plt.legend(fontsize=20) #显示图例
#
# plt.savefig("多字.png")
# plt.show() #显示作图结果


#直方图
# import matplotlib.pyplot as plt
# import numpy as np
# import matplotlib
#
# # 设置matplotlib正常显示中文和负号
# matplotlib.rcParams['font.sans-serif']=['SimHei']   # 用黑体显示中文
# matplotlib.rcParams['axes.unicode_minus']=False     # 正常显示负号
# # 随机生成（10000,）服从正态分布的数据
# data = np.random.randn(10000)
# """
# 绘制直方图
# data:必选参数，绘图数据
# bins:直方图的长条形数目，可选项，默认为10
# normed:是否将得到的直方图向量归一化，可选项，默认为0，代表不归一化，显示频数。normed=1，表示归一化，显示频率。
# facecolor:长条形的颜色
# edgecolor:长条形边框的颜色
# alpha:透明度
# """
# plt.hist(multiword, bins=10000, facecolor="blue", edgecolor="black", alpha=0.7)
# # 显示横轴标签x
# plt.ylim(0, 500) #显示的y轴范围
# plt.xlim([0.0,1])
# plt.xticks([0,0.001,0.005],fontsize=20)  # 调调字体
# # plt.xlim([0.0,0.001,0.1,1.0,2.0,3.0,4.0,100.0])
# plt.xlabel("区间")
# # 显示纵轴标签
# plt.ylabel("频数/频率")
# # 显示图标题
# plt.title("频数/频率分布直方图")
# plt.show()


# 统计图
from matplotlib.ticker import FuncFormatter
import matplotlib.pyplot as plt
import numpy as np


x = [0.0, 0.0025, 0.005, 0.0075, 0.01,0.0325, 0.055 , 0.0775, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1,3.25,  5.5 ,  7.75, 10.0,20.0,30.0,  50.0,  60.0,
        100.0,200.0,1000000]
dict_loss = {}
n = len(correctwords)
all_n = len(multiword)+len(lesswords)+len(errorwords)+len(correctwords)
multiword = np.array(correctwords)
all_pro = round(len(correctwords)/all_n*100,2)
dict_loss['type'] = 'correctwords'+'  '+ str(all_pro)+'%'
for i in range(len(x) - 1):
    value = len(multiword[(multiword >= x[i]) & (multiword < x[i + 1])])
    proportion = round(value/n*100,2)#当前类型内占比
    proportion1 = round(value/all_n*100,2)#当前类型占所有数据的占比
    key = str(x[i])+'-'+str(x[i+1])
    dict_loss[key]=str(value)+'  '+str(proportion)+'%'+'  '+str(proportion1)+'%'
print(dict_loss)
